package com.xin.ruanjian.omsystem.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.adapter.MainRvAdapter;
import com.xin.ruanjian.omsystem.bases.BaseFragment;
import com.xin.ruanjian.omsystem.bean.IndexBean;
import com.xin.ruanjian.omsystem.utils.HttpUtils;

import butterknife.BindView;
import okhttp3.Call;

import static com.xin.ruanjian.omsystem.bases.Contants.SHOP_LIST;

/**
 * Created by local123 on 2018/2/2.
 */

public class MainFragment extends BaseFragment {
    @BindView(R.id.rv_fragment_main)
    RecyclerView rvFragmentMain;

    private onScrollListener onScrollListener;
    private int page = 1;
    private MainRvAdapter adapter;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    protected void initView(View view) {
        adapter = new MainRvAdapter(getActivity());
        rvFragmentMain.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        rvFragmentMain.setAdapter(adapter);
        rvFragmentMain.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0) {
                    onScrollListener.upScroll();
                }else {
                    onScrollListener.downScroll();
                }
            }
        });
    }

    @Override
    protected void initData() {
        showLoadingDialog();
        HttpUtils httpUtils = new HttpUtils(SHOP_LIST) {
            @Override
            public void onError(Call call, Exception e, int id) {
                showErrorDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                IndexBean bean = new Gson().fromJson(response, IndexBean.class);
                Log.i("====", "onResponse: " + response);
                if (bean.getState() == 1) {
                    if (page == 1) {
                        adapter.setData(bean);
                    }else {
                        adapter.addData(bean.getMsg().getShop());
                    }
                    page ++;
                }else {
                    if (page == 1) {
                        adapter.setData(bean);
                    }else {
                        adapter.addData(bean.getMsg().getShop());
                    }
                }
            }
        };
        httpUtils.addParam("page", page+"");
        httpUtils.clicent();
    }

    public void setOnScrollListener(MainFragment.onScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public interface onScrollListener {
        void upScroll();
        void downScroll();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }
}
